package view.systemAdmiView;

import java.awt.EventQueue;
import java.sql.Connection;
import java.sql.ResultSet;

import javax.swing.JInternalFrame;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.ImageIcon;
import javax.swing.JTextField;
import javax.swing.LayoutStyle.ComponentPlacement;

import dao.BookDao;
import dao.BookTypeDao;
import dao.UserDao;
import model.BookType;
import model.User;
import util.DbUtil;
import util.StringUtil;

import javax.swing.JComboBox;
import javax.swing.JRadioButton;
import javax.swing.JButton;
import javax.swing.ButtonGroup;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.Font;
import java.awt.Color;

public class UserImfAddInterFrm extends JInternalFrame {
	private JTextField userNameTxt;
	private JTextField passWordTxt;
	private JTextField telTxt;
	private JTextField emailTxt;
	private JRadioButton manJrb;
	private JRadioButton femaleJrb;
	
	private JComboBox userTypeJcb ;
	private DbUtil dbUtil=new DbUtil();
	private UserDao userDao=new UserDao();
	
	
	
	private final ButtonGroup buttonGroup = new ButtonGroup();

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					UserImfAddInterFrm frame = new UserImfAddInterFrm();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public UserImfAddInterFrm() {
		getContentPane().setBackground(new Color(230, 230, 250));
		setIconifiable(true);
		setClosable(true);
		setTitle("用户信息添加");
		setBounds(100, 100, 1267, 754);
		
		JLabel userName = new JLabel("用户名：");
		userName.setFont(new Font("宋体", Font.BOLD, 30));
		userName.setIcon(new ImageIcon(UserImfAddInterFrm.class.getResource("/images/systemAdmi/用户名.png")));
		
		userNameTxt = new JTextField();
		userNameTxt.setFont(new Font("宋体", Font.BOLD, 30));
		userNameTxt.setColumns(10);
		
		JLabel passWord = new JLabel("密 码：");
		passWord.setFont(new Font("宋体", Font.BOLD, 30));
		passWord.setIcon(new ImageIcon(UserImfAddInterFrm.class.getResource("/images/systemAdmi/密码 (1).png")));
		
		passWordTxt = new JTextField();
		passWordTxt.setFont(new Font("宋体", Font.BOLD, 30));
		passWordTxt.setColumns(10);
		
		JLabel userType = new JLabel("用户类型：");
		userType.setFont(new Font("宋体", Font.BOLD, 30));
		userType.setIcon(new ImageIcon(UserImfAddInterFrm.class.getResource("/images/systemAdmi/用户名.png")));
		
		JLabel tel = new JLabel("手机号码：");
		tel.setFont(new Font("宋体", Font.BOLD, 30));
		tel.setIcon(new ImageIcon(UserImfAddInterFrm.class.getResource("/images/systemAdmi/手机号码.png")));
		
		telTxt = new JTextField();
		telTxt.setFont(new Font("宋体", Font.BOLD, 30));
		telTxt.setColumns(10);
		
		JLabel sex = new JLabel("性 别：");
		sex.setFont(new Font("宋体", Font.BOLD, 30));
		sex.setIcon(new ImageIcon(UserImfAddInterFrm.class.getResource("/images/systemAdmi/性别 (1).png")));
		
		manJrb = new JRadioButton("男");
		manJrb.setFont(new Font("宋体", Font.BOLD, 30));
		buttonGroup.add(manJrb);
		manJrb.setSelected(true);
		
		
		femaleJrb = new JRadioButton("女");
		femaleJrb.setFont(new Font("宋体", Font.BOLD, 30));
		buttonGroup.add(femaleJrb);
		
		JButton addButton = new JButton("添加");
		addButton.setIcon(new ImageIcon(UserImfAddInterFrm.class.getResource("/images/bookAdmi/添加 (2).png")));
		addButton.setFont(new Font("宋体", Font.BOLD, 30));
		addButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				userAddActionPerformed(e);
			}
		});
		
		JButton cancelButton = new JButton("取消");
		cancelButton.setFont(new Font("Dialog", Font.BOLD, 25));
		cancelButton.setIcon(new ImageIcon(UserImfAddInterFrm.class.getResource("/images/bookAdmi/重置.png")));
		cancelButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				resetValueActionPerformed(e);
			}
		});
		
		JLabel lblNewLabel_5 = new JLabel("用户信息添加");
		lblNewLabel_5.setFont(new Font("宋体", Font.BOLD, 40));
		lblNewLabel_5.setIcon(new ImageIcon(UserImfAddInterFrm.class.getResource("/images/systemAdmi/用户添加.png")));
		
		JLabel email = new JLabel("邮  箱：");
		email.setFont(new Font("宋体", Font.BOLD, 30));
		email.setIcon(new ImageIcon(UserImfAddInterFrm.class.getResource("/images/systemAdmi/邮件.png")));
		
		emailTxt = new JTextField();
		emailTxt.setFont(new Font("宋体", Font.BOLD, 30));
		emailTxt.setColumns(10);
		
		String []types= {"学生","老师","系统管理员","图书管理员"};
		userTypeJcb = new JComboBox(types);
		userTypeJcb.setFont(new Font("宋体", Font.BOLD, 30));
		
		
		GroupLayout groupLayout = new GroupLayout(getContentPane());
		groupLayout.setHorizontalGroup(
			groupLayout.createParallelGroup(Alignment.TRAILING)
				.addGroup(groupLayout.createSequentialGroup()
					.addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
						.addGroup(groupLayout.createSequentialGroup()
							.addGroup(groupLayout.createParallelGroup(Alignment.LEADING, false)
								.addGroup(groupLayout.createSequentialGroup()
									.addGap(46)
									.addGroup(groupLayout.createParallelGroup(Alignment.LEADING, false)
										.addGroup(groupLayout.createSequentialGroup()
											.addComponent(tel)
											.addPreferredGap(ComponentPlacement.RELATED)
											.addComponent(telTxt, GroupLayout.PREFERRED_SIZE, 237, GroupLayout.PREFERRED_SIZE))
										.addGroup(groupLayout.createSequentialGroup()
											.addComponent(userName)
											.addPreferredGap(ComponentPlacement.UNRELATED)
											.addComponent(userNameTxt))
										.addGroup(groupLayout.createSequentialGroup()
											.addComponent(passWord)
											.addPreferredGap(ComponentPlacement.RELATED)
											.addComponent(passWordTxt)))
									.addGap(40))
								.addGroup(Alignment.TRAILING, groupLayout.createSequentialGroup()
									.addContainerGap(GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
									.addComponent(addButton)
									.addGap(21)))
							.addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
								.addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
									.addGroup(groupLayout.createSequentialGroup()
										.addGap(219)
										.addComponent(sex)
										.addGap(18)
										.addComponent(manJrb)
										.addGap(38)
										.addComponent(femaleJrb))
									.addGroup(groupLayout.createSequentialGroup()
										.addGap(205)
										.addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
											.addGroup(groupLayout.createSequentialGroup()
												.addComponent(email)
												.addGap(28)
												.addComponent(emailTxt, GroupLayout.PREFERRED_SIZE, 239, GroupLayout.PREFERRED_SIZE))
											.addGroup(groupLayout.createSequentialGroup()
												.addComponent(userType)
												.addPreferredGap(ComponentPlacement.RELATED)
												.addComponent(userTypeJcb, GroupLayout.PREFERRED_SIZE, 249, GroupLayout.PREFERRED_SIZE)))))
								.addGroup(groupLayout.createSequentialGroup()
									.addGap(139)
									.addComponent(cancelButton))))
						.addGroup(groupLayout.createSequentialGroup()
							.addGap(403)
							.addComponent(lblNewLabel_5)))
					.addContainerGap(144, Short.MAX_VALUE))
		);
		groupLayout.setVerticalGroup(
			groupLayout.createParallelGroup(Alignment.LEADING)
				.addGroup(groupLayout.createSequentialGroup()
					.addContainerGap()
					.addComponent(lblNewLabel_5, GroupLayout.PREFERRED_SIZE, 108, GroupLayout.PREFERRED_SIZE)
					.addPreferredGap(ComponentPlacement.RELATED)
					.addGroup(groupLayout.createParallelGroup(Alignment.TRAILING)
						.addGroup(groupLayout.createParallelGroup(Alignment.BASELINE)
							.addComponent(userName)
							.addComponent(userNameTxt, GroupLayout.PREFERRED_SIZE, 41, GroupLayout.PREFERRED_SIZE))
						.addGroup(groupLayout.createSequentialGroup()
							.addGroup(groupLayout.createParallelGroup(Alignment.BASELINE)
								.addComponent(sex)
								.addComponent(manJrb)
								.addComponent(femaleJrb))
							.addPreferredGap(ComponentPlacement.RELATED)))
					.addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
						.addGroup(groupLayout.createSequentialGroup()
							.addGap(78)
							.addGroup(groupLayout.createParallelGroup(Alignment.BASELINE)
								.addComponent(passWord)
								.addComponent(passWordTxt, GroupLayout.PREFERRED_SIZE, 47, GroupLayout.PREFERRED_SIZE)))
						.addGroup(groupLayout.createSequentialGroup()
							.addGap(43)
							.addComponent(userType, GroupLayout.PREFERRED_SIZE, 100, GroupLayout.PREFERRED_SIZE))
						.addGroup(groupLayout.createSequentialGroup()
							.addGap(81)
							.addComponent(userTypeJcb, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)))
					.addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
						.addGroup(groupLayout.createSequentialGroup()
							.addGap(74)
							.addGroup(groupLayout.createParallelGroup(Alignment.BASELINE)
								.addComponent(tel)
								.addComponent(telTxt, GroupLayout.PREFERRED_SIZE, 42, GroupLayout.PREFERRED_SIZE)
								.addComponent(email))
							.addGap(82)
							.addGroup(groupLayout.createParallelGroup(Alignment.BASELINE)
								.addComponent(addButton)
								.addComponent(cancelButton)))
						.addGroup(groupLayout.createSequentialGroup()
							.addGap(93)
							.addComponent(emailTxt, GroupLayout.PREFERRED_SIZE, 42, GroupLayout.PREFERRED_SIZE)))
					.addContainerGap(116, Short.MAX_VALUE))
		);
		getContentPane().setLayout(groupLayout);

		//设置JFrame居中显示
		this.setLocation(300, 30);
	}

	/**
	 * 取消事件处理
	 * @param e
	 */
	private void resetValueActionPerformed(ActionEvent e) {
		this.reSetValue();
		
	}

	/**
	 * 用户信息添加处理事件
	 * @param e
	 */
	private void userAddActionPerformed(ActionEvent evt) {
		String userName=this.userNameTxt.getText();
		String passWord=this.passWordTxt.getText();
		String userType=(String) this.userTypeJcb.getSelectedItem();
		String tel=this.telTxt.getText();
		String email=this.emailTxt.getText();

		
		if(StringUtil.isEmpty(userName)) {
			JOptionPane.showMessageDialog(null, "用户名不能为空!");
			return;
		}
		if(StringUtil.isEmpty(passWord)) {
			JOptionPane.showMessageDialog(null, "用户密码不能为空!");
			return;
		}
		if(StringUtil.isEmpty(userType)) {
			JOptionPane.showMessageDialog(null, "用户类型不能为空!");
			return;
		}
		if(StringUtil.isEmpty(tel)) {
			JOptionPane.showMessageDialog(null, "用户手机号不能为空!");
			return;
		}
		if(StringUtil.isEmpty(email)) {
			JOptionPane.showMessageDialog(null, "用户邮件不能为空!");
			return;
		}
		
		
		String sex="";
		if(manJrb.isSelected()) {
			sex="男";
			
		}
		else if(femaleJrb.isSelected()) {
			sex="女";
		}
		
		
		User user=new User(userName,passWord,sex,userType,tel,email);
		
		Connection con=null;
		try {
			con=dbUtil.getCon();
			int addNum=userDao.add(con, user);
			if(addNum==1) {
				JOptionPane.showMessageDialog(null, "用户信息添加成功!");
				reSetValue();
				
			}
			else {
				JOptionPane.showMessageDialog(null, "用户信息添加失败!");				
				
			}
			
		}catch(Exception e) {
			e.printStackTrace();
			JOptionPane.showMessageDialog(null, "用户信息添加失败!");		
		}finally {
			try {
				dbUtil.closeCon(con);
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		
	   }
  }
	
	
	/**
	 * 重置表单
	 */
	private void reSetValue() {
		this.userNameTxt.setText("");
		this.passWordTxt.setText("");
		
		this.manJrb.setSelected(true);
		this.telTxt.setText("");
		this.emailTxt.setText("");
		
		if(this.userTypeJcb.getItemCount()>0) {
			this.userTypeJcb.setSelectedIndex(0);
			
		}
		
	}
}
